package com.rocket.bedroom.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rocket.bedroom.exceptions.RepeatNameException;
import com.rocket.bedroom.exceptions.RoomException;
import com.rocket.bedroom.pojo.BlockEntity;
import com.rocket.bedroom.pojo.FloorEntity;
import com.rocket.bedroom.service.IFloorService;
import com.rocket.bedroom.service.IRoomService;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 周毅
 * @ClassName:
 * @Description: TODO
 * @Date: Create in 10:51 2020/5/4
 * @ModifyBy:
 */
@RestController
@RequestMapping(value = "/floor")
public class FloorController {

    @Resource
    private IFloorService floorService;

    @Resource
    private IRoomService roomService;

    @GetMapping(value = "/pageFloor")
    public PageInfo<FloorEntity> pageFloor(FloorEntity floorEntity, Integer pageNumber, Integer pageSize) {
        if (null == pageNumber || 0 >= pageNumber) {
            pageNumber = 1;
        }
        if (null == pageSize || 0 >= pageSize) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);


        LambdaQueryWrapper<FloorEntity> lambdaWrapper = Wrappers.lambdaQuery();
        if (null != floorEntity) {
            if (null != floorEntity.getBlockId()) {
                lambdaWrapper.eq(FloorEntity::getBlockId, floorEntity.getBlockId());
            }
            if (!StringUtils.isEmpty(floorEntity.getFloorName())) {
                lambdaWrapper.likeRight(FloorEntity::getFloorName, floorEntity.getFloorName());
            }
        }
        List<FloorEntity> list = floorService.list(lambdaWrapper);
        return new PageInfo<>(list);
    }

    @PostMapping(value = "/addFloorAndRoomBerth")
    public int addFloorAndRoomBerth(FloorEntity floorEntity, Integer roomNumber) {
        //根据楼层批量添加房间及床位
        boolean saveRoom = roomService.addRoomAndBethByFloor(floorEntity, roomNumber);

        return saveRoom ? 1 : -1;
    }

    @GetMapping(value = "/findFloorById/{floorId}")
    public FloorEntity findFloorById(@PathVariable("floorId") Long floorId) {
        Assert.notNull(floorId, "FIND FloorId IS NULL");
        FloorEntity findFloor = floorService.getById(floorId);
        return findFloor;
    }

    @PostMapping(value = "/addFloor")
    public int addFloor(FloorEntity floorEntity) {
        Assert.notNull(floorEntity, "ADD Floor IS NULL");
        Assert.notNull(floorEntity.getBlockId(), "ADD Floor BlockId IS NULL");
        Assert.notNull(floorEntity.getFloorName(), "ADD  Floor BlockId IS NULL");

        if (0 != floorService.hasFloorName(floorEntity.getFloorName(), floorEntity.getBlockId())) {
            throw new RepeatNameException("楼层存在重复", 500);
        }
        boolean saveFloor = floorService.save(floorEntity);
        return saveFloor ? 1 : -1;
    }


}
